package com.tangmu.rabbits.ui.activity.home;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.tangmu.rabbits.R;
import com.tangmu.rabbits.adapter.home.TodayHotsAdapter;
import com.tangmu.rabbits.base.BaseActivity;
import com.tangmu.rabbits.constants.Constants;
import com.tangmu.rabbits.ui.fragment.home.HomeFragment;
import com.tangmu.rabbits.ui.fragment.home.today.BeBeingFragment;
import com.tangmu.rabbits.ui.fragment.home.today.RankNextFragment;
import com.tangmu.rabbits.ui.fragment.home.today.RankingHotsFragment;
import com.tangmu.rabbits.ui.fragment.mine.MineFragment;
import com.tangmu.rabbits.ui.fragment.optimization.OptimizationFragment;
import com.tangmu.rabbits.ui.fragment.search.SearchFragment;
import com.tangmu.rabbits.ui.fragment.ticket.TicketFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 今日必选
 */
public class TodayChooseActivity extends BaseActivity {

    @BindView(R.id.hots_recycler)
    RecyclerView mRecyclerView;
    //RadioGroup
    @BindView(R.id.rb_today_ranking)
    RadioButton rbTodayRanking;
    @BindView(R.id.rb_today_going)
    RadioButton rbTodayGoing;
    @BindView(R.id.rb_today_next)
    RadioButton rbTodayNext;
    @BindView(R.id.rg_today)
    RadioGroup rgToday;

    private List<Integer> datas = new ArrayList<>();
    private TodayHotsAdapter adapter;

    private RankingHotsFragment hotsFragment;
    private BeBeingFragment beBeingFragment;
    private RankNextFragment nextFragment;
    private FragmentTransaction ft;

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.statusBarColor(R.color.transparent).titleBar(R.id.headerView).init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_today_choose;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initView() {
        setHeaderImage(Constants.Position.LEFT, R.drawable.icon_back, false, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setHeaderTitle("今日必选");
        setHeaderImage(Constants.Position.RIGHT, R.drawable.icon_refresh, false, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        for (int i = 0; i < 3; i++) {
            datas.add(i);
        }

        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        adapter = new TodayHotsAdapter(mContext, datas);
        mRecyclerView.setAdapter(adapter);

        setSwitchFragment(0);

        rgToday.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_today_ranking:
                        setSwitchFragment(0);
                        break;
                    case R.id.rb_today_going:
                        setSwitchFragment(1);
                        break;
                    case R.id.rb_today_next:
                        setSwitchFragment(2);
                        break;
                }
            }
        });

    }


    private void setSwitchFragment(int i) {
        ft = getSupportFragmentManager().beginTransaction();
        hideFragments(ft);
        switch (i) {
            case 0:
                hotsFragment = (RankingHotsFragment) getSupportFragmentManager().findFragmentByTag(RankingHotsFragment.TAG);
                if (hotsFragment == null) {
                    hotsFragment = new RankingHotsFragment();
                    ft.add(R.id.today_layout_container, hotsFragment, RankingHotsFragment.TAG);
                } else {
                    ft.show(hotsFragment);
                }
                break;
            case 1:
                beBeingFragment = (BeBeingFragment) getSupportFragmentManager().findFragmentByTag(BeBeingFragment.TAG);
                if (beBeingFragment == null) {
                    beBeingFragment = new BeBeingFragment();
                    ft.add(R.id.today_layout_container, beBeingFragment, BeBeingFragment.TAG);
                } else {
                    ft.show(beBeingFragment);
                }
                break;
            case 2:
                nextFragment = (RankNextFragment) getSupportFragmentManager().findFragmentByTag(RankNextFragment.TAG);
                if (nextFragment == null) {
                    nextFragment = new RankNextFragment();
                    ft.add(R.id.today_layout_container, nextFragment, RankNextFragment.TAG);
                } else {
                    ft.show(nextFragment);
                }
                break;

        }
        ft.commit();
    }

    private void hideFragments(FragmentTransaction ft) {
        if (hotsFragment != null) {
            ft.hide(hotsFragment);
        }
        if (beBeingFragment != null) {
            ft.hide(beBeingFragment);
        }

        if (nextFragment != null) {
            ft.hide(nextFragment);
        }
    }

}
